package com.example.template.controller;

import com.example.template.dto.AccountDto;
import com.example.template.entity.Account;
import com.example.template.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author BuerYouth 账户查询
 */
@Slf4j
@RestController
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 去账户查询
     */
    @GetMapping("/account")
    public ModelAndView account(ModelAndView mv, @RequestParam("token") String token) {
        log.info("查询接收到token->" + token);
        mv.setViewName("account");
        mv.addObject("token", token);
        return mv;
    }

    /**
     * 去新增用户
     */
    @GetMapping("/toAddUser")
    public ModelAndView addAccount(ModelAndView mv, @RequestParam("token") String token) {
        log.info("新增接收到token->" + token);
        mv.setViewName("addUser");
        mv.addObject("token", token);
        return mv;
    }

    /**
     * 查看全部信息
     */
    @GetMapping("/loadData")
    public Map<String, Object> loadData(ModelAndView mv, @RequestParam("token") String token) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "success");
        result.put("count", 1000);
        result.put("data", accountService.loadDataAll());
        return result;
    }

    /**
     * 查询信息
     */
    @GetMapping("/loadDataSearch")
    public Map<String, Object> loadDataSearch(ModelAndView mv, @RequestParam("token") String token,
        @RequestParam(value = "username", required = false) String username) {
        System.out.println("userName:" + username);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "success");
        result.put("count", 1000);
        if (username.isEmpty()) {
            result.put("data", accountService.loadDataAll());
        } else {
            result.put("data", accountService.loadDataSearch(username));
        }
        return result;
    }

    /**
     * 添加信息
     */
    @PostMapping("/addData")
    public Map<String, Object> addData(ModelAndView mv, @RequestParam("token") String token, AccountDto accountDto) {
        System.out.println(accountDto.toString());
        Account account = new Account();
        account.setUserid(accountDto.getUserid());
        account.setUsername(accountDto.getUsername());
        account.setUseremail(accountDto.getUseremail());
        account.setPassword(accountDto.getPassword());
        accountService.addData(account);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "添加成功");
        result.put("count", 1000);
        result.put("data", "");
        return result;
    }

}
